/**
 * @Author: 罗显
 * @CreateTime: 2025-05-08
 * @Version: 1.0
 */


package com.it.dataservice.user.controller;

import com.it.common.config.ResponseResult;
import com.it.common.config.ResultBean;
import com.it.dataservice.user.entity.UserEntity;
import com.it.dataservice.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController("/app")
public class UserController {

    @Autowired
    private UserService userService;

    //密码登录
    @PostMapping("/login")
    public ResponseEntity<ResponseResult<Object>> login(@RequestBody UserEntity userEntity) {
        if (userEntity == null || userEntity.getPhone() == null || userEntity.getPassword() == null) {
            return ResponseEntity.badRequest().body(ResponseResult.error(ResultBean.NOT_NULL.value(), ResultBean.NOT_NULL.description()));
        }else{
            UserEntity user = userService.login(userEntity.getPhone(), userEntity.getPassword());
            if (user == null) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(ResponseResult.error(ResultBean.LOGIN_FAILED.value(), "用户名不存在"));
            }else{
                return ResponseEntity.ok(ResponseResult.success(user));
            }
        }
    }
}